using System;
using System.Collections.Generic;
using Commons.Models.Locks;
using Commons.Models.Settings.Dtos.Locks;
using Commons.Models.Settings.Matchers.Operators;
using Commons.Models.Stats;

namespace Commons.Models.Settings.Mappers.Locks
{
	public class UserStatsUnlockConditionMapper : IMapper<IUnlockConditionDto, UnlockCondition>
	{
		private RelationalOperatorMapper<int> operatorMapper = new RelationalOperatorMapper<int>();

		public UnlockCondition Map (IUnlockConditionDto config)
		{
			UserStatsUnlockConditionDto statsLockDto = (UserStatsUnlockConditionDto)config;

			IRelationalOperator<int> relationalOperator = operatorMapper.Map(statsLockDto.Operator);

			UserStatsUnlockCondition unlockCondition = new UserStatsUnlockCondition(relationalOperator);

			if (statsLockDto.Lifes != 0)
			{
                unlockCondition.AddRequiredValue(StatUnit.Lifes, statsLockDto.Lifes);
			}

            if (statsLockDto.Floor != 0)
            {
                unlockCondition.AddRequiredValue(StatUnit.Floor, statsLockDto.Floor);
            }

            if (statsLockDto.Score != 0)
            {
                unlockCondition.AddRequiredValue(StatUnit.Score, statsLockDto.Score);
            }

			if (statsLockDto.Credits != 0)
			{
				unlockCondition.AddRequiredValue(StatUnit.Credits, statsLockDto.Credits);
			}

			if (statsLockDto.Xp != 0)
			{
				unlockCondition.AddRequiredValue(StatUnit.Xp, statsLockDto.Xp);
			}

			if (statsLockDto.XpLevel != 0)
			{
				unlockCondition.AddRequiredLevel(StatUnit.Xp, statsLockDto.XpLevel);
			}

			if (unlockCondition.GetRequiredValuesUnits().Count + unlockCondition.GetRequiredLevelsUnits().Count <= 0)
			{
				throw new Exception("stats unlock condition can't be empty");
			}

			return unlockCondition;
		}
	}
}

